/*
* @Description: 访问后端的用户接口
* @LastEditors: kcz
* @LastEditTime: 2021-01-07 09:09:01
*/
import 'package:dio/dio.dart';
import 'package:edu/constants/config.dart';
import 'package:edu/networks/account/models/user_response.dart';
import 'package:edu/networks/common/base_response.dart';
import 'package:retrofit/retrofit.dart';

part 'user_api.g.dart';

enum UserLoginType {
  emailPassword,
  emailVerifyCode,
  phonePassword,
  phoneVerifyCode,
}

abstract class UserApi {
  // 登录
  Future<UserBriefResponse> login({
    required int type,
    required String account,
    required String code,
    CancelToken? cancelToken,
  });

  // 退出登录
  Future<SimpleResponse> logout();

  // 获取验证码
  Future<SimpleResponse> verifyCodeForLogin({
    required String type,
    required String account,
    CancelToken? cancelToken,
  });

  // 获取用户简要信息
  Future<UserBriefResponse> getUserBrief();

  // 用户详细信息
  Future<UserProfileResponse> getUserProfile();

  // 更新用户头像
  Future<UserProfileFieldResponse> updateUserAvatar(List<int> bytes);

  // 更新用户昵称
  Future<UserProfileFieldResponse> updateUserNickname(String nickname);

  // 更新用户职业
  Future<ProfessionResponse> getAllProfessions({CancelToken? cancelToken});

  Future<UserProfileFieldResponse> updateUserProfession(int professionId);

  // 更新用户性别
  Future<UserProfileFieldResponse> updateUserGender(String gender);

  // 更新用户所属地区或地址
  Future<ChinaRegionResponse> getChinaRegions({
    String? code,
    CancelToken? cancelToken,
  });

  Future<UserProfileFieldResponse> updateUserRegion(String code);

  // 更新用户签名
  Future<UserProfileFieldResponse> updateUserIntroduction(String introduction);

  // 修改用户邮箱
  Future<UserProfileFieldResponse> updateUserEmail(
    String email,
    String verifyCode,
  );
  Future<VerifyCodeStatusResponse> changeUserEmail(String email);

  // 修改用户手机号
  Future<UserProfileFieldResponse> updateUserPhoneNumber(
    String phoneNumber,
    String verifyCode,
  );
  Future<VerifyCodeStatusResponse> changeUserPhoneNumber(String phoneNumber);

  // 忘记密码/找回密码/重置密码
  Future<VerifyCodeStatusResponse> changeUserPassword(
    String type,
    String account,
  );
  Future<SimpleResponse> updateUserPassword({
    required String type,
    String? account,
    required String password,
    required String code,
  });

  static int getLoginType(UserLoginType type) {
    const values = UserLoginType.values;
    return values.indexOf(type);
  }
}

@RestApi(baseUrl: Config.baseUrl)
abstract class RemoteUserApi extends UserApi {
  factory RemoteUserApi(Dio dio, {String baseUrl}) = _RemoteUserApi;

  // 登录
  @override
  @POST('/v1/user/login')
  Future<UserBriefResponse> login({
    @Field('type') required int type,
    @Field('account') required String account,
    @Field('code') required String code,
    @CancelRequest() CancelToken? cancelToken,
  });

  // 退出登录
  @override
  @POST('/v1/user/logout')
  Future<SimpleResponse> logout();

  // 用户登录验证码验证
  @override
  @POST('/v1/user/login/verify_code')
  Future<SimpleResponse> verifyCodeForLogin({
    @Field('type') required String type,
    @Field('account') required String account,
    @CancelRequest() CancelToken? cancelToken,
  });

  // 获取用户简要信息
  @override
  @GET('/v1/user/brief')
  Future<UserBriefResponse> getUserBrief();

  // 用户详细信息
  @override
  @GET('/v1/user/profile')
  Future<UserProfileResponse> getUserProfile();

  // 更新用户头像
  @override
  @PATCH('/v1/user/avatar')
  Future<UserProfileFieldResponse> updateUserAvatar(
    @Part(name: 'avatar', fileName: 'avatar.jpg') List<int> bytes,
  );

  // 更新用户昵称
  @override
  @PATCH('/v1/user/nickname')
  Future<UserProfileFieldResponse> updateUserNickname(
    @Field('nickname') String nickname,
  );

  // 更新用户职业
  @override
  @PATCH('/v1/user/profession')
  Future<UserProfileFieldResponse> updateUserProfession(
    @Field('profession_id') int professionId,
  );

  // 获取所有职业
  @override
  @GET('/v1/user/professions')
  Future<ProfessionResponse> getAllProfessions({
    @CancelRequest() CancelToken? cancelToken,
  });

  // 更新用户性别
  @override
  @PATCH('/v1/user/gender')
  Future<UserProfileFieldResponse> updateUserGender(
    @Field('gender') String gender,
  );

  // 更新用户所属地区或地址
  @override
  @PATCH('/v1/user/region')
  Future<UserProfileFieldResponse> updateUserRegion(@Field('code') String code);

  @override
  @GET('/v1/user/chinaregions')
  Future<ChinaRegionResponse> getChinaRegions({
    @Query('code') String? code,
    @CancelRequest() CancelToken? cancelToken,
  });

  // 更新用户签名
  @override
  @PATCH('/v1/user/introduction')
  Future<UserProfileFieldResponse> updateUserIntroduction(
    @Field('introduction') String introduction,
  );

  // 修改用户邮箱
  @override
  @PATCH('/v1/user/email')
  Future<UserProfileFieldResponse> updateUserEmail(
    @Field('email') String? email,
    @Field('verify_code') String? verifyCode,
  );

  @override
  @POST('/v1/user/email/change')
  Future<VerifyCodeStatusResponse> changeUserEmail(
    @Field('email') String? email,
  );

  // 修改用户手机号
  @override
  @PATCH('/v1/user/phone_number')
  Future<UserProfileFieldResponse> updateUserPhoneNumber(
    @Field('phone_number') String? phoneNumber,
    @Field('verify_code') String? verifyCode,
  );

  @override
  @POST('/v1/user/phone_number/change')
  Future<VerifyCodeStatusResponse> changeUserPhoneNumber(
    @Field('phone_number') String? phoneNumber,
  );

  // 忘记密码/找回密码/重置密码
  @override
  @POST('/v1/user/password/change')
  Future<VerifyCodeStatusResponse> changeUserPassword(
    @Field('type') String type,
    @Field('account') String account,
  );

  @override
  @PATCH('/v1/user/password')
  Future<SimpleResponse> updateUserPassword({
    @Field('type') required String type,
    @Field('account') String? account,
    @Field('password') required String password,
    @Field('code') required String code,
  });
}
